import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		/**
		 * 是否需要强制登录
		 */
		forcedLogin: false,
		hasLogin: uni.getStorageSync('hasLogin') ? uni.getStorageSync('hasLogin') : false,
		channelLogin: uni.getStorageSync('channelLogin') ? uni.getStorageSync('channelLogin') : false,
		timHasLogin: uni.getStorageSync('timLogin') ? uni.getStorageSync('timLogin') : false,
		token: uni.getStorageSync('token') ? uni.getStorageSync('token') : '',
		member: uni.getStorageSync('member') ? uni.getStorageSync('member') : '',

		isTIMLogin: false,
		isSDKReady: false, // TIM SDK 是否 ready

		conversationActive: {}, //聊天进行中的会话
		toUserId: '', //聊天对象id
		toGroupId:'',//
		conversationList: [], //会话列表
		currentMessageList: [], //消息列表
	},
	mutations: {
		login(state, data) {
			state.token = data[0] || ''
			state.hasLogin = true

			uni.clearStorageSync()
			uni.setStorageSync('token', state.token)
			uni.setStorageSync('hasLogin', state.hasLogin)
		},
		channelLogin(state, data) {
			state.channelLogin = true
			uni.setStorageSync('channelLogin', state.channelLogin)
		},
		timLogin(state, data) {
			state.timLogin = true
			uni.setStorageSync('timLogin', state.timLogin)
		},
		info(state, data) {
			state.member = (data)
			uni.setStorageSync('member', state.member)
		},
		channelLogout(state) {
			state.channelLogin = false
			uni.setStorageSync('channelLogin', state.channelLogin)
		},
		timLogout(state) {
			state.isTIMLogin = false
			state.isSDKReady = false
			uni.setStorageSync('timLogin', state.timLogin)
		},
		logout(state) {
			state.token = ""
			state.hasLogin = false
			state.member = ""

			uni.setStorageSync('token', state.token)
			uni.setStorageSync('member', state.token)
			uni.setStorageSync('hasLogin', state.hasLogin)
			uni.clearStorageSync()
		},
		//更新登录状态
		toggleIsLogin(state, isTIMLogin) {
		  state.isTIMLogin = typeof isTIMLogin === 'undefined' ? !state.isTIMLogin : isTIMLogin
		},
		//更新TIMSDK状态
		toggleIsSDKReady(state, isSDKReady) {
		  state.isSDKReady = typeof isSDKReady === 'undefined' ? !state.isSDKReady : isSDKReady
		},
		//退出登录重置状态
		reset(state) {
			state.isTIMLogin = false
			state.isSDKReady = false
		},
		//选择好友聊天--创建会话/拼接会话id
		createConversationActive(state,toUserId){
			state.conversationActive.conversationID = 'C2C'+toUserId
			state.toUserId = toUserId
			state.currentMessageList = []
		},
		createGroupConversationActive(state, toGroupId){
			state.conversationActive.conversationID = 'GROUP'+toGroupId
			state.toGroupId = toGroupId
			state.currentMessageList = []
		},
		//选择已有会话聊天--更新选中会话详情
		updateConversationActive(state,conversationItem){
			state.conversationActive = Object.assign({}, conversationItem.conversation);
			state.toUserId = conversationItem.user.userId
			state.currentMessageList = []
		},
		//更新会话列表
		updateConversationList(state,newConversationList){
			state.conversationList = newConversationList
		},
		/**
		 * 将消息插入当前会话列表
		 * 调用时机：收/发消息事件触发时
		 * @param {Object} state
		 * @param {Message[]|Message} data
		 * @returns
		 */
		pushCurrentMessageList(state, data) {
		  // 还没当前会话，则跳过
		  if (Array.isArray(data)) {
		    // 筛选出当前会话的消息
		    const result = data.filter(item => item.conversationID === state.conversationActive.conversationID)
		    state.currentMessageList = [...state.currentMessageList, ...result]
		  } else if (data.conversationID === state.conversationActive.conversationID) {
		    state.currentMessageList = [...state.currentMessageList, data]
		  }
		  console.log('1111')
		  	console.log( state.currentMessageList)
		},
		/**
		 * 滑到顶部请求更多的历史消息
		 * */
		unshiftCurrentMessageList(state,data){
			console.log(data)
			if(data){
				state.currentMessageList = [...data,...state.currentMessageList]
			}
		},
	}
})

export default store
